package com.yuews.codegenerator.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 配置管理器 - 单例模式
 */
public class ConfigManager {
    private static ConfigManager instance;
    private Properties properties;

    private ConfigManager() {
        properties = new Properties();
    }

    public static synchronized ConfigManager getInstance() {
        if (instance == null) {
            instance = new ConfigManager();
        }
        return instance;
    }

    /**
     * 加载配置文件
     */
    public void loadConfig(String configPath) throws IOException {
        try (FileInputStream fis = new FileInputStream(configPath)) {
            properties.load(fis);
        }
    }

    /**
     * 获取基础包名
     */
    public String getBasePackage() {
        return properties.getProperty("base.package", "com.example");
    }

    /**
     * 获取输出路径
     */
    public String getOutputPath() {
        return properties.getProperty("output.path", "./src/main/java");
    }

    /**
     * 是否覆盖现有文件
     */
    public boolean isOverwriteExisting() {
        return Boolean.parseBoolean(properties.getProperty("overwrite.existing", "false"));
    }

    /**
     * 获取模板目录
     */
    public String getTemplateDir() {
        return properties.getProperty("template.dir", "./templates");
    }

    /**
     * 获取实体类模板
     */
    public String getEntityTemplate() {
        return properties.getProperty("template.entity", "entity.ftl");
    }

    /**
     * 获取Mapper接口模板
     */
    public String getMapperTemplate() {
        return properties.getProperty("template.mapper", "mapper.ftl");
    }

    /**
     * 获取XML映射模板
     */
    public String getXmlTemplate() {
        return properties.getProperty("template.xml", "xml.ftl");
    }

    /**
     * 获取Service接口模板
     */
    public String getServiceTemplate() {
        return properties.getProperty("template.service", "service.ftl");
    }

    /**
     * 获取Service实现模板
     */
    public String getServiceImplTemplate() {
        return properties.getProperty("template.serviceImpl", "serviceImpl.ftl");
    }

    /**
     * 获取Controller模板
     */
    public String getControllerTemplate() {
        return properties.getProperty("template.controller", "controller.ftl");
    }

    /**
     * 是否生成Controller
     */
    public boolean shouldGenerateController() {
        return Boolean.parseBoolean(properties.getProperty("generate.controller", "false"));
    }
    
    /**
     * 获取解析器类型
     */
    public String getParserType() {
        return properties.getProperty("parser.type", "sql");
    }
    
    /**
     * 获取数据库连接信息
     */
    public String getDatabaseConnectionInfo() {
        return properties.getProperty("database.connection.info", "");
    }
    
    /**
     * 获取表名到实体类名的映射配置
     * 配置格式：table.entity.mapping=表名1:实体类名1,表名2:实体类名2
     */
    public java.util.Map<String, String> getTableEntityMapping() {
        java.util.Map<String, String> mapping = new java.util.HashMap<>();
        String mappingConfig = properties.getProperty("table.entity.mapping", "");
        
        if (!mappingConfig.trim().isEmpty()) {
            String[] pairs = mappingConfig.split(",");
            for (String pair : pairs) {
                String[] parts = pair.split(":");
                if (parts.length == 2) {
                    String tableName = parts[0].trim();
                    String entityName = parts[1].trim();
                    if (!tableName.isEmpty() && !entityName.isEmpty()) {
                        mapping.put(tableName, entityName);
                    }
                }
            }
        }
        
        return mapping;
    }
}